#include "traymenu.h"
#include "config.h"

#include <QApplication>
#include <QMessageBox>
#include <QDir>
#include <QDate>


static int mwIndex = 0;

TrayMenu::TrayMenu(QWidget *parent) : QMenu(parent)
{
    manageWindow = new ManageWindow;
    pManageAction = new QAction(tr("管理便签"));
#if 0
    this->addAction(pManageAction);
    this->addSeparator();
#endif
    pQuitAction = new QAction(tr("退出"));
	this->addAction(pQuitAction);

    createDataDir();
    confJsonFile = new QFile(CONF_FILE_PATH);
    loadConfigJson();

	connect(pQuitAction, &QAction::triggered,
			this, &TrayMenu::on_pQuitAction_triggered);
    connect(pManageAction, &QAction::triggered,
            this, &TrayMenu::on_pManageAction_triggered);

    startTimer(200);
}

void TrayMenu::createDataDir()
{
    QDir tmpDir;
    if (!tmpDir.exists(DATA_DIR_PATH)) {
        tmpDir.mkpath(DATA_DIR_PATH);
    }

}

/*Create a default config file*/
void TrayMenu::newConfigJson()
{
    QDir tmpDir;
    if (!tmpDir.exists(CONF_DIR_PATH)) {
        tmpDir.mkpath(CONF_DIR_PATH);
    }
    confJsonFile->open(QFile::NewOnly);
    confJsonFile->close();
}

void TrayMenu::newNoteConf(int xPos, int yPos)
{
    QJsonObject newJsonObj;
    newJsonObj.insert("File", DATA_FILE_NAME);
    newJsonObj.insert("ShowLineNumber", true);
    newJsonObj.insert("ShowToolBar", false);
    newJsonObj.insert("StayOnTop", false);
    newJsonObj.insert("Xpos", xPos);
    newJsonObj.insert("Ypos", yPos);
    newJsonObj.insert("Hidden", false);
    confJsonArray.append(newJsonObj);
}

void TrayMenu::loadConfigJson()
{
    QFile dataFile;
    if (!confJsonFile->exists()) {
        newConfigJson();
    }
    confJsonFile->open(QFile::ReadOnly);
    confJsonArray = QJsonDocument::fromJson(confJsonFile->readAll()).array();
    confJsonFile->close();
    if (0 == confJsonArray.count()) {
        newNoteConf(DEFAULT_MW_XPOS, DEFAULT_MW_YPOS);
    }
    for (int i = 0; i < confJsonArray.count(); i++) {
/*window*/
        auto mw = new MainWindow(mwIndex, confJsonArray.at(i).toObject());
        mwIndex++;
        mws.append(mw);
        connect(mw, &MainWindow::newWindow, this, &TrayMenu::on_mws_newWindow);
        connect(mw, &MainWindow::closeWindow, this, &TrayMenu::on_mws_closeWindow);
    }

}

void TrayMenu::on_mws_newWindow()
{
    auto pre_mw = static_cast<MainWindow *>(sender());
    int newXPos = pre_mw->jsonConf()["Xpos"].toInt() + pre_mw->width();
    int newYPos = pre_mw->jsonConf()["Ypos"].toInt();
    pre_mw = nullptr;
    delete pre_mw;
    QJsonObject newJsonObj;
    newNoteConf(newXPos, newYPos);
    MainWindow *mw = new MainWindow(mwIndex,
                                    confJsonArray.at(confJsonArray.size() - 1).toObject());
    mwIndex++;
    mws.append(mw);
    connect(mw, &MainWindow::newWindow, this, &TrayMenu::on_mws_newWindow);
    connect(mw, &MainWindow::closeWindow, this, &TrayMenu::on_mws_closeWindow);
}

void TrayMenu::on_mws_closeWindow()
{
    QFile tmpDataFile;
    auto mw = static_cast<MainWindow *>(sender());
    mws.removeAt(mw->indexOfList());
    mwIndex = mws.size() - 1;
    mw->close();
    mw = nullptr;
    delete mw;
    for (int i = 0; i < mws.count(); i++) {
        mws.at(i)->setIndexOfList(i);
    }
}

void TrayMenu::on_pQuitAction_triggered()
{
    qApp->quit();
}

void TrayMenu::on_pManageAction_triggered()
{
    manageWindow->wShow();
}


void TrayMenu::timerEvent(QTimerEvent *event)
{
#if 1
    while (confJsonArray.count() != 0) {
        confJsonArray.removeFirst();
    }
    for (int i = 0; i < mws.count(); i++) {
        confJsonArray.append(mws.at(i)->jsonConf());
    }
    confJsonFile->open(QFile::WriteOnly);
    confJsonFile->write(QJsonDocument(confJsonArray).toJson());
    confJsonFile->close();
    event->accept();
#endif
}
